package com.yomahub.liteflow.example.controller.zip;

import cn.hutool.core.map.MapUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.yomahub.liteflow.example.LiteflowExampleApplication;
import com.yomahub.liteflow.example.config.UploadFtpConfig;
import com.yomahub.liteflow.example.utils.HdfsUtil;
import com.yomahub.liteflow.example.utils.PackUtils;
import com.yomahub.liteflow.example.utils.SshServerUtils;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: freemarker-project
 * @description: \
 * @author: yyyyjinying
 * @create: 2023-12-02 10:19
 **/
@RestController
@RequestMapping("zip")
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {HdfsUtil.class, LiteflowExampleApplication.class})
public class GzipController {

    @Autowired
    private PackUtils packUtils;

    @Autowired
    private HdfsUtil hdfsUtil;

    // ftp保存list字符串
    @Test
    public void uploadListStrGZ() {
        // List<String> list = Arrays.asList("我爱你", "中国!");
        List<String> rawdata = new ArrayList<>();
        rawdata.add("1057797fdfdfdfdf玩排位匹配");
        rawdata.add("1057797fdfdfdfdf玩排位匹配");
        rawdata.add("1057797fdfdfdfdf玩排位匹配");
        rawdata.add("1057797fdfdfdfdf玩排位匹配");
        rawdata.add("1057797fdfdfdfdf玩排位匹配");
        rawdata.add("1057797fdfdfdfdf玩排位匹配");
        rawdata.add("1057797fdfdfdfdf玩排位匹配");
        rawdata.add("1057797fdfdfdfdf玩排位匹配");
        rawdata.add("1057797fdfdfdfdf玩排位匹配");
        rawdata.add("1057797fdfdfdfdf玩排位匹配");
        rawdata.add("1057797fdfdfdfdf玩排位匹配");
        rawdata.add("1057797w0u0--04wc00-Yofqo076B00qo005+ooovoio12No7Zc0100020002");
        // rawdata.add("1057797w0u0--04wc00-Yofqo076B00qo005+ooovoio12No7Zc0100020002");
        // rawdata.add("1057897w0u0--04wc00-Yofqo076B00qo005+ooovoio12No7Zc0100020002");
        // rawdata.add("1057997w0u0--04wc00-Yofqo076B00qo005+ooovoio12No7Zc0100020002");
        // rawdata.add("1058097w0u0--04wc00-Yofqo076B00qo005+ooovoio12No7Zc0100020002");
        // rawdata.add("1058197w0u0--04wc00-Yofqo076B00qo005+ooovoio12No7Zc0100020002");
        // rawdata.add("1058297w0u0--04wc00-Yofqo076B00qo005+ooovoio12No7Zc0100020002");
        // rawdata.add("1058397w0u0--04wc00-Yofqo076B00qo005+ooovoio12No7Zc0100020002");
        // rawdata.add("1058497w0u0--04wc00-Yofqo076B00qo005+ooovoio12No7Zc0100020002");
        // rawdata.add("1058597w0u0--04wc00-Yofqo076B00qo005+ooovoio12No7Zc0100020002");
        boolean b = packUtils.uploadListStrToFTP(rawdata, "/home/shfileuser/der_qar_flight/demo8.gz");
        // boolean b = packUtils.uploadListStrToFTP(rawdata, "/ftp/demo8.gz");
        System.out.println(b);
    }

    // ftp读取list字符串
    @Test
    public void getListStrGZ() {
        List<String> listStrFromFile = packUtils.getListStrFromFTP("/home/shfileuser/der_qar_flight/B1016-a496ce6679a047ea90c6eb8e16caefdd-ZGSZ-ZSQD-20230719-102752.gz");
        // List<String> listStrFromFile = packUtils.getListStrFromFTP("/ftp/demo8.gz");
        System.out.println(listStrFromFile);
    }


    // ftp下载文本
    @Test
    public void downfileGZ() {
        packUtils.getFileFromFTP("/ftp/demo8.gz");
    }

    public static Map<String, String> getFilenamePreSuffex(String pathall) {
        String fileName = new File(pathall).getName();
        String dir = pathall.substring(0, pathall.lastIndexOf(fileName));
        Map<String, String> map = new HashMap<>();
        map.put("name", fileName.substring(0, pathall.lastIndexOf(".")));
        map.put("dir", dir);
        return map;
    }

    @Test
    public void demo() {
        getFilenamePreSuffex("/ftp/demo8.gz");
    }

    @Test
    public void demoPatten() {
        String inputString = "name=x['${VRTG}'],name='_World2',name='_World3',name='_World4',name='_World5'";
        // String regex = "name='[_a-zA-Z]+'";
        String regex = "\\{(.*?)\\}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(inputString);
        while (matcher.find()) {
            String matchedValue = matcher.group(1);
            System.out.println(matchedValue); // 输出 "World"
        }

        String str = "Hello x['VRTG'], Hello x['VRTG1']";
        String replaced = str.replaceAll("'(.*?)'", "'Replacement'");

        System.out.println(replaced);


        // String inputString = "这是一个 '示例' 字符串";
        // String regex = "'(.*?)'"; // 使用非贪婪模式匹配单引号中的内容
        //
        // Pattern pattern = Pattern.compile(regex);
        // Matcher matcher = pattern.matcher(inputString);
        // if (matcher.find()) {
        //     String matchedValue = matcher.group(1);
        //     System.out.println(matchedValue); // 输出 "示例"
        // }
    }

    @Test
    public void demoPatten2() {
        String inputString = "l_kv_vrtg_bndry = keyValue['#KP_VRTG_BNDRY']";
        // String regex = "name='[_a-zA-Z]+'";
        String regex = "keyValue\\['(.*?)'\\]";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(inputString);
        while (matcher.find()) {
            String matchedValue = matcher.group(1);
            System.out.println(matchedValue); // 输出 "World"
        }
    }

    @Autowired
    private SshServerUtils sshServerUtils;

    @Test
    public void jssshdemo() {
        sshServerUtils.mkdir("/home/ftpuser/nn/bb.txt");
    }

    @Test
    public void hdfsdemo() {
        // hdfsUtil.mkdir("/aaa");
        // hdfsUtil.getPathAllFiles();
        hdfsUtil.moveLocalFile(new Path("E:\\tmp\\csv\\f089a2e0e78443a4a0fd477855d1fc73-B5357-20231029-171842.csv"), new Path("/aaa"));
    }

    @Test
    public void hdfsCsvdemo() {
        List<Map<String, String>> list = new ArrayList<>();
        list.add(MapUtil.<String, String>builder().put("rownum", "1").put("name", "zhao").put("age", "20").build());
        list.add(MapUtil.<String, String>builder().put("rownum", "2").put("name", "zhao").put("age", "20").build());
        list.add(MapUtil.<String, String>builder().put("rownum", "3").put("name", "zhao").put("age", "20").build());
        list.add(MapUtil.<String, String>builder().put("rownum", "4").put("name", "zhao").put("age", "20").build());
        list.add(MapUtil.<String, String>builder().put("rownum", "5").put("name", "zhao").put("age", "20").build());
        list.add(MapUtil.<String, String>builder().put("rownum", "6").put("name", "zhao").put("age", "20").build());
        hdfsUtil.writeListMapToCSV("id,name,age", list, "/aaa/test.csv");
    }

    public String demolistt(){
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        for (int i = 0; i < list.size(); i++) {
            // 在Lambda表达式中的代码放置可能抛出自定义异常的逻辑
            if (i == 3) {
                throw new RuntimeException("自定义异常");
            }
        }
        return "aaa";

    }

    @Test
    public void demoList(){
        try {
            String demolistt = demolistt();
            System.out.println(demolistt);

        } catch (Exception e) {
            // 捕获其他异常
            System.out.println("捕获到异常：" + e.getMessage());
        }
    }

}
